<template>
	<view :data-theme="theme">

		<view class="picker-view" @click="openAddress">
			<view class="city">
				{{shaText}} >
			</view>
		</view>


		<liu-choose-address ref="scroll" :cityArr="originCityArr" @change='chooseSuccess'></liu-choose-address>

		<view class='apply-index'>
			<view class='list pad30' v-for="(item,index) in cityArr" :key="index">
				<view class='item'>
					<view class='data1'>区域名称: {{item.name}}</view>
					<view class='data2'>总人口总数: {{item.population}}</view>
				</view>
				<!-- agentStatus:状态(0-申请，1、2-待审批（不可点击），9-已经代理（不可点击）) -->
				<view class="btn">

					<button v-if="item.agentStatus==0" class="light" @click="getAgentApply(item)">申请</button>
					<button v-else-if="item.agentStatus==1" class="gray">待审批</button>
					<button v-else class="gray">已经代理</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAgentApply,
		getCityList
	} from '@/api/user.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	let app = getApp();
	export default {
		data() {
			return {
				loading: false,
				loadend: false,
				loadtitle: '加载更多',
				page: 1,
				limit: 8,
				signList: [],
				theme: app.globalData.theme,
				cityArr: [],
				originCityArr: [],
				pickerDic: {},
				shaText: '省市筛选'
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getAgentApply();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getCityList()
			} else {
				toLogin();
			}
		},
		onReachBottom: function() {},
		methods: {
			//打开地址选择器
			openAddress() {
				this.$refs.scroll.open()
			},
			//地址选择成功
			chooseSuccess(e) {
				console.log('所选择的地址信息:', e)
				let dataDic = e.value[e.value.length - 1]
				
				this.shaText=e.value[e.value.length-1].mergerName
				
				
				this.pickerDic = dataDic
				this.pickerCityList()
				debugger
			},
			
			pickerCityList(){
				
				let that = this
				let params = {
					parentId: this.pickerDic.cityId ? this.pickerDic.cityId: 0
				}
				that.loading = true;
				that.loadtitle = "";
				getCityList(params).then(res => {
					console.log(res)
					that.cityArr = res.data.list
				})
			},
			
			
			getCityList() {
				let that = this
				let params = {
					parentId: 0
				}
				that.loading = true;
				that.loadtitle = "";
				getCityList(params).then(res => {
					console.log(res)
					that.cityArr = res.data.list
					that.originCityArr = res.data.list
					let loadend = that.cityArr.length < that.limit;
					that.loadend = loadend;
					that.loading = false;
					// that.loadtitle = loadend ? "哼😕~我也是有底线的~" : "加载更多"				

				})
			},


			/**
			 * 申请代理商
			 */
			getAgentApply(item) {

				let that = this;				
				uni.showModal({
					title: '是否提交代理申请？',
					success(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							getAgentApply({
								parentId: item.id,
								cityId: item.cityId
							}).then(res => {
								
								if(res.code == 200){
									that.$util.Tips({
										title: '提交成功 请耐心等待审核！'
									});
									
									that.pickerCityList()
								}else{
									that.$util.Tips({
										title: res.message
									});
								}
								
								
							}).catch(err => {
								that.loading = false;
								that.loadtitle = '加载更多';
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})




			},
		}
	}
</script>

<style lang="scss">
	.font_color {
		@include main_color(theme);
	}

	page {
		background-color: #F5F5F5;
	}

	.apply-index {
		margin: 0rpx 20rpx;
		padding-bottom: 30rpx;

		.pad30 {
			padding: 30rpx;
		}

		.list {
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20rpx 0rpx;
			background-color: white;

			.item {
				font-size: 24rpx;

				.data1 {}

				.data2 {
					padding-top: 10rpx;
				}
			}

			.btn {
				& button {
					font-size: 30rpx;
				}

				.light {
					color: #1DB0FC;
				}

				.gray {
					color: lightgray;
				}
			}

		}



	}




	.picker-view {
		background-color: white;

		.city {
			display: inline-block;
			// width: 50%;
			// text-align: center;
			padding: 20rpx;
			padding-left: 40rpx;
			font-size: 24rpx;
		}
	}
</style>